package com.cos.shop.service.impl;

import com.cos.shop.common.Const;
import com.cos.shop.common.ResponseResult;
import com.cos.shop.dao.*;
import com.cos.shop.dao.impl.*;
import com.cos.shop.entity.Item;
import com.cos.shop.entity.Orders;
import com.cos.shop.entity.vo.CartVo;
import com.cos.shop.entity.vo.OrdersVO;
import com.cos.shop.service.OrdersServices;
import com.cos.shop.util.RandomUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Copyright (C),2021-2022,亚瑟王
 * FileName: OrdersServicesImpl
 * Author: saber
 * Date:2022/9/20 12:38
 */
public class OrdersServicesImpl implements OrdersServices {
    private OrdersDao od = new OrdersDaoImpl();
    private CartDao cd = new CartDaoImpl();
    private ProductDao pd = new ProductDaoImpl();
    private AddressDao ad = new AddressDaoImpl();
    private ItemDao id = new ItemDaoImpl();

    @Override
    public ResponseResult addOrders(int aid, int uid) {
        try {
            if (aid>0){
                Orders orders = new Orders();
                orders.setOid(RandomUtils.createOrderId());
                orders.setAid(aid);
                orders.setUid(uid);
                orders.setOtime(new Date());
                orders.setOstate(Const.OrderCode.NOT_PAY);

                List<CartVo> cartVos = cd.getCartAll(uid);
                if (cartVos!=null&&cartVos.size()>0) {
                    double sum = 0;
                    List<Item> items = new ArrayList<>();
                    for (CartVo cartVo:cartVos){
                        Item item = new Item(orders.getOid(),cartVo.getPid(),cartVo.getCcount(),cartVo.getCnum());
                        sum+=cartVo.getCcount();
                        items.add(item);
                    }
                    orders.setOcount(sum);
                    od.addOrders(orders);
                    id.addItemlist(items);

                    cd.deleteCart(uid);
                    return ResponseResult.createSuccess(orders);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("订单创建失败");
    }

    @Override
    public ResponseResult getAllOrders(int uid) {
        try {
            List<OrdersVO> ordersVOList = od.getAllByUid(uid);
            return ResponseResult.createSuccess(ordersVOList);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("订单查询失败");
    }

    @Override
    public Orders getOrdersDetailByOid(String oid) {
        try {
            OrdersVO orders = od.getOrdersByOid(oid);
            return orders;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public ResponseResult chageStatus(String oid) {
        try {
            OrdersVO orders = od.getOrdersByOid(oid);

            if (orders.getOstate()== Const.OrderCode.IS_SEND) {
                od.changeStatus(oid,Const.OrderCode.ENCOMMENT);
                return ResponseResult.createSuccess("已收货,请您对商品做出评价");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("该订单无法收货,有问题请联系客服处理");
    }
}
